package com.test.webflux.config;

import com.test.webflux.controller.ProductController;
import com.test.webflux.controller.UserController;
import com.test.webflux.entity.Product;
import com.test.webflux.entity.ResponseResult;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * 路由配置 - 按单个路由配置
 * @Description
 * @Author lingmin
 * @Date 2025/4/14 11:24
 **/
//@Configuration
public class WebFluxConfig {


    /**
     * 路由1
     * @param userController
     * @return
     */
    @Bean
    public RouterFunction helloRouterFunction(UserController userController) {
        return RouterFunctions.route(
                RequestPredicates.GET("/public/hello").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                request -> ServerResponse.ok().bodyValue("Public Hello")
        );
    }

    /**
     * 路由1
     * @param userController
     * @return
     */
    @Bean
    public RouterFunction userTitleRouterFunction(UserController userController) {
        return RouterFunctions.route(
                RequestPredicates.GET("/user/title").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                //request -> ServerResponse.ok().bodyValue("用户") //可以成功调用
                request -> ServerResponse.ok().body(userController.getTitle() , String.class)
        );
    }

    /**
     * 路由2
     * @param userController
     * @return
     */
    @Bean
    public RouterFunction usersRouterFunction(UserController userController) {
        return RouterFunctions.route(
                RequestPredicates.POST("/user/list").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                request -> ServerResponse.ok().body(userController.list() , ResponseResult.class)
        );
    }

    /**
     * 路由1
     * @param productController
     * @return
     */
    @Bean
    public RouterFunction productTitleRouterFunction(ProductController productController) {
        return RouterFunctions.route(
                RequestPredicates.GET("/product/title").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                //request -> ServerResponse.ok().bodyValue("用户") //可以成功调用
                request -> ServerResponse.ok().body(productController.getTitle() , String.class)
        );
    }

    /**
     * 路由2
     * @param productController
     * @return
     */
    @Bean
    public RouterFunction productRouterFunction(ProductController productController) {
        return RouterFunctions.route(
                RequestPredicates.POST("/product/list").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                request -> {
                    return request.bodyToMono(Product.class).flatMap(product -> {
                        return ServerResponse.ok().body(productController.list(product) , ResponseResult.class);
                    });
                }
        );
    }

    /**
     * 路由1
     * @param productController
     * @return
     */
    @Bean
    public RouterFunction productSaveRouterFunction(ProductController productController) {
        return RouterFunctions.route(
                RequestPredicates.POST("/product/save").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                request -> ServerResponse.ok().body(productController.save(request.bodyToMono(Product.class)) , ResponseResult.class)
        );
    }

    /**
     * 路由1
     * @param productController
     * @return
     */
    @Bean
    public RouterFunction subscribeRouterFunction(ProductController productController) {
        return RouterFunctions.route(
                RequestPredicates.POST("/product/subscribe").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)),
                request -> ServerResponse.ok().build(productController.subscribe(request.bodyToMono(Product.class)))
        );
    }


}